package cn.HuanG.quartz.job;

import cn.HuanG.base.constant.OrderTypeConstants;
import cn.HuanG.order.domain.OrderAdopt;
import cn.HuanG.order.domain.OrderProduct;
import cn.HuanG.order.service.IOrderAdoptService;
import cn.HuanG.order.service.IOrderProductService;
import cn.HuanG.pay.domain.PayBill;
import cn.HuanG.pay.service.IPayBillService;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Map;

@Component
public class MainJob implements Job {
    @Autowired
    private IPayBillService payBillService;
    @Autowired
    private IOrderAdoptService orderAdoptService;
    @Autowired
    private IOrderProductService orderProductService;
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        /**
                获取定时任务传来的参数：
                1、根据订单号  查询t_pay_bill
                2、更改状态  更新时间
                3、根据业务类型  查询对应的订单  t_order_adopt
                4、更改里面的状态
                5、根据t_order_adopt 里面的字段pet_id  找到对应的宠物上架
         */
        JobDataMap jobDataMap = jobExecutionContext.getJobDetail().getJobDataMap();
        Map<String,Object> map= (Map<String, Object>) jobDataMap.get("params");
        String orderSn = (String) map.get("orderSn");
        //1.根据订单号 查询t_pay_bill
        PayBill payBill =payBillService.loadByOrderSn(orderSn);
        //2.更改 状态  更新时间
        payBill.setState(-1);//已支付的状态
        payBill.setUpdateTime(new Date());
        payBillService.update(payBill);
        //3.根据 业务类型 查询对应的 订单 t_order_adopt
        // 3.根据业务类型 查询对应的订单 OrderAdopt
        switch (payBill.getBusinessType()){
            case OrderTypeConstants.ORDER_ADOPT:
                OrderAdopt orderAdopt =orderAdoptService.queryById(payBill.getBusinessKey());
                //4.更改里面的状态
                orderAdopt.setState(-1);//订单状态
                orderAdoptService.update(orderAdopt);
                break;
            case OrderTypeConstants.ORDER_GOODS:
                break;
            case OrderTypeConstants.ORDER_PRODUCT:
                OrderProduct orderProduct = orderProductService.queryById(payBill.getBusinessKey());
                orderProduct.setState(-1);
                orderProductService.update(orderProduct);
                break;
        }
    }
}
